package br.com.obardavez.util.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import javax.persistence.Id;

import br.com.obardavez.util.CollectionUtil;
import br.com.obardavez.util.StringUtil;
import br.com.obardavez.util.Util;

import com.google.inject.Inject;

public class CollectionUtilImpl implements Serializable, CollectionUtil {

	private static final long serialVersionUID = 1L;

	@Inject
	private Util util;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.CollectionUtil#join(java.util.Collection)
	 */
	@Override
	public String join(Collection<?> texts) {
		return join(texts, StringUtil.DEFAULT_DELIMITER);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.CollectionUtil#join(java.util.Collection,
	 * java.lang.String)
	 */
	@Override
	public String join(Collection<?> texts, String delimiter) {
		String joinedText = "";
		if (texts != null) {
			for (Object text : texts) {
				joinedText += joinedText.equals("") ? "" : delimiter;
				joinedText += text.toString();
			}
		}
		return joinedText;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.CollectionUtil#join(java.lang.String[])
	 */
	@Override
	public String join(String[] texts) {
		String joinedText = null;
		if (texts != null) {
			joinedText = join(Arrays.asList(texts),
					StringUtil.DEFAULT_DELIMITER);
		}
		return joinedText;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.CollectionUtil#join(java.lang.String[],
	 * java.lang.String)
	 */
	@Override
	public String join(String[] texts, String delimiter) {
		String joinedText = "";
		if (texts != null) {
			joinedText = join(Arrays.asList(texts), delimiter);
		}
		return joinedText;
	}
	
	public <E> String joinIdEntity(Collection<E> collection) {
		return joinIdEntity(collection, StringUtil.DEFAULT_DELIMITER);
	}

	public <E> String joinIdEntity(Collection<E> collection, String delimiter) {
		List<Long> ids = new ArrayList<Long>();
		if(collection != null){
			for (E entity : collection) {
				ids.add( (Long) util.getBeanUtil().getPropertyValue(entity, Id.class));
			}
		}
		return join(ids, delimiter);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.CollectionUtil#isEmpty(java.lang.Object[])
	 */
	@Override
	public Boolean isEmpty(Object[] array) {
		Boolean isEmpty = Boolean.TRUE;
		
		if(array != null  && array.length > 0){
			for (Object object : array) {
				if(object != null){
					isEmpty = Boolean.FALSE;
					break;
				}
			}
		}
		
		return isEmpty ;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.CollectionUtil#isNotEmpty(java.lang.Object
	 * [])
	 */
	@Override
	public Boolean isNotEmpty(Object[] array) {
		return !isEmpty(array);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.CollectionUtil#isEmpty(java.util.Collection
	 * )
	 */
	@Override
	public Boolean isEmpty(Collection<?> items) {
		Boolean isEmpty = Boolean.TRUE;
		
		if(items != null  && items.size() > 0){
			for (Object object : items) {
				if(object != null){
					isEmpty = Boolean.FALSE;
					break;
				}
			}
		}
		return isEmpty;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.CollectionUtil#isNotEmpty(java.util.Collection
	 * )
	 */
	@Override
	public Boolean isNotEmpty(Collection<?> items) {
		return !isEmpty(items);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.CollectionUtil#asList(java.util.Collection
	 * )
	 */
	@Override
	public <T> List<T> asList(Collection<T> items) {
		List<T> list = new ArrayList<T>();
		if (items != null) {
			list.addAll(items);
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.CollectionUtil#asList(T[])
	 */
	@Override
	public <T> List<T> asList(T[] items) {
		List<T> list = new ArrayList<T>();
		if (items != null) {
			list.addAll(Arrays.asList(items));
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.CollectionUtil#getUtil()
	 */
	@Override
	public Util getUtil() {
		return util;
	}

}
